<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Auth extends MY_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function login() {
        $error = 0;
        if ($this->session->userdata('email')) {
            redirect('admin/home');
        }
        if ($this->input->post('submit')) {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $this->load->model('user/User_model', 'user_model');
            $user = $this->user_model->checkLogin($username, $this->encode_pwd($password));
            if ($user) {
                $this->session->set_userdata('username', $username);
                //$this->session->set_userdata('role', $user->role_type);
                $this->session->set_userdata('user_id', $user->id);
                $this->system_log->log(': Login as ' . $username);
                redirect('/admin/home');
                die;
            } else {
                $error = 1;
            }
        }
        $this->load->view('login', array('error' => $error));
    }

    function logout() {
        $this->session->unset_userdata('username');
        $this->session->unset_userdata('user_id');
        redirect('/user/auth/login');
    }

}
